from constants import *
from helpers import sec2text, breaks_and_colors
import tkinter
from tkinter import messagebox
# from plyer import notification

reps = 0
timer = ""

def start_timer():
    global reps
    answer = "yes"
    status = "work" if reps % 2 == 0 else "breake"
    if reps > 0:
        answer = messagebox.askquestion('Action', f'Want to {status} now?')
    if answer == "yes":
        reps += 1
        text, color, mins = breaks_and_colors(reps)
        start_btn["state"] = "disabled"
        reset_btn["state"] = "normal"
        countdown(mins * 60)
        title.config(text=text, fg=color)
        rounds = reps // 2
        marks = "✔" * rounds
        checkmark.config(text=marks)
    else:
        title.config(text=f"Next is {status}", fg=GREEN)


def reset_timer():
    window.after_cancel(timer)
    canvas.itemconfig(timer_text, text="00:00")
    start_btn["state"] = "normal"
    reset_btn["state"] = "disabled"
    global reps
    reps -= 1

def countdown(sec):
    canvas.itemconfig(timer_text, text=sec2text(sec))
    if sec > 0:
        global timer
        timer = window.after(1000, countdown, sec - 1)
    else:
        start_btn["state"] = "normal"
        # notification.notify(title="Privet", app_name="Pymodoro", message="Hello guy", timeout=10)
        reset_btn["state"] = "disabled"
        start_timer()


window = tkinter.Tk()
window.title("Pymodoro timer")
window.config(pady=50, padx=100, bg=YELLOW)


title = tkinter.Label(text="Timer", bg=YELLOW, fg=GREEN,
                      font=("Aerial", 35, "bold"))
title.grid(row=0, column=1)

canvas = tkinter.Canvas(width=WIDTH, height=HEIGHT,
                        bg=YELLOW, highlightthickness=0)


tomato_img = tkinter.PhotoImage(file="tomato.png")
canvas.create_image(WIDTH / 2, HEIGHT / 2, image=tomato_img)
timer_text = canvas.create_text(
    WIDTH / 2, HEIGHT / 2 + 15, text="00:00", fill="white", font=(FONT_NAME, 35, "bold"))
canvas.grid(row=1, column=1)

start_btn = tkinter.Button(
    text="Start", command=start_timer, highlightthickness=0)
start_btn.grid(row=2, column=0)

checkmark = tkinter.Label(pady=20, text="", bg=YELLOW,
                    fg=GREEN, font=("Aerial", 16))
checkmark.grid(row=2, column=1)

reset_btn = tkinter.Button(
    text="Reset", state="disabled", command=reset_timer, highlightthickness=0)
reset_btn.grid(row=2, column=2)
window.mainloop()
